/**
 * Created with IntelliJ IDEA.
 * User: ajhin
 * Date: 3/9/14
 * Time: 2:28 PM
 * To change this template use File | Settings | File Templates.
 */
package com.societyGames.sortEngine
{
import flash.display.Sprite;

public class SortScene
{
  private var _sprite:Sprite = new Sprite();

  public function get sprite():Sprite
  {
    return this._sprite;
  }

  public function addItem(item:SortItem):void
  {
    if (item == null)
    {
      throw new ArgumentError("Argument \"item\" cannot be null.");
    }

    if (item.sortScene != null)
    {
      item.sortScene.removeItem(item);
      item.sortScene = null;
    }

    item.sortScene = this;

    //Put the item at the end then sort it.
    item.index = this._sprite.numChildren;
    this._sprite.addChild(item);

    updateSort(item);
  }

  public function removeItem(item:SortItem):void
  {
    if (item.sortScene != this || item.index < 0 || item.index >= this._sprite.numChildren)
    {
      throw new ArgumentError("Argument item was not a member of this SortScene or it had an invalid index.");
    }

    for (var otherIndex:int = item.index + 1; otherIndex < this._sprite.numChildren; otherIndex++)
    {
      SortItem(this._sprite.getChildAt(otherIndex)).index--;
    }

    this._sprite.removeChildAt(item.index);

    item.sortScene = null;
    item.index = -1;
  }

  //index 0  1  2
  //zsort 1  2  3
  //zsort 1  4  3
  //zsort 1  0  3

  public function updateSort(sortItem:SortItem):void
  {
    if (this._sprite.getChildAt(sortItem.index) != sortItem) //Check to make sure the sortItem is in the right place.
    {
      throw new ArgumentError("Argument sortItem was not a member of this SortScene or had its sort index changed.");
    }

    var otherSortItem:SortItem; //The neighbor sort item that we're comparing against.
    var index:int; //Where we're at right now.

    //Look for greater neighbors.
    if (sortItem.index < this._sprite.numChildren)
    {
      for (index = sortItem.index + 1; index <  this._sprite.numChildren; index++) //Look for greater neighbors
      {
        otherSortItem = SortItem(this._sprite.getChildAt(index));
        if (sortItem.zSort > otherSortItem.zSort)
        {
          otherSortItem.index--;
        }
        else
        {
          index--;
          break;
        }
      }
      if (index == this._sprite.numChildren)
      {
        index--;
      }
      if (index > sortItem.index)
      {
        sortItem.index = index;
        this._sprite.addChildAt(sortItem, index);
      }
    }

    //Look for lesser neighbors.
    if (sortItem.index > 0)
    {
      for (index = sortItem.index - 1; index >=  0; index--)
      {
        otherSortItem = SortItem(this._sprite.getChildAt(index));
        if (sortItem.zSort < otherSortItem.zSort)
        {
          otherSortItem.index++;
        }
        else
        {
          index++;
          break;
        }
      }
      if (index == -1)
      {
        index++;
      }
      if (index < sortItem.index)
      {
        sortItem.index = index;
        this._sprite.addChildAt(sortItem, index);
      }
    }
  }
}
}
